//
//  GrowDataManager.m
//  leo
//
//  Created by 常艳 on 15/12/10.
//  Copyright (c) 2015年 mdx. All rights reserved.
//

#import "GrowDataManager.h"


@implementation GrowDataManager
+(GrowDataManager *)sharedSingleton
{
    static GrowDataManager *growDataManager;
    @synchronized(self)
    {
        if(growDataManager==NULL)
            growDataManager=[[GrowDataManager alloc] init];
    }
    return growDataManager;
}
-(id)init
{
    self = [super init];
    self.theHelper=[[DBHelper alloc] init];
    return  self;
}
-(void)dealloc
{
    self.theHelper=NULL;
}
-(BOOL) checkValid
{
    if(self.theHelper==NULL)
    {
        NSLog(@"%@,checkValid,theHelper is null",GrowDataManager_TAG);
        return NO;
    }
    return YES;
}
//UI更改
//获取所有系统任务列表
-(NSArray *)getSysTasks
{
    if([self checkValid]==NO)
        return NULL;
    NSString *nsql=[NSString stringWithFormat:@"select * from sys_task;"];
    NSArray *data=[self.theHelper searchData:[nsql UTF8String]];
    if(data==NULL||[data count]==0)
    {
        NSLog(@"%@,getSysTasks,data is null.",GrowDataManager_TAG);
        return NULL;
    }
    NSMutableArray *taskArray=[[NSMutableArray alloc] init];
    for(int i=0;i<[data count];i++)
    {
        NSDictionary *taskInfo=[data objectAtIndex:i];
        if(taskInfo==NULL||[taskInfo count]==0)
            continue;
        Task *task=[[Task alloc] init];
        task.taskId=[taskInfo valueForKey:@"task_id"];
        task.type=[[taskInfo valueForKey:@"type"] intValue];
        task.expireTs=[[taskInfo valueForKey:@"expire"] intValue];
        task.title=[taskInfo valueForKey:@"title"];
        task.desc=[taskInfo valueForKey:@"description"];
        task.empirical=[[taskInfo valueForKey:@"empirical"] intValue];
        task.status=[[taskInfo valueForKey:@"status"] intValue];
        task.audioUrl=[taskInfo valueForKey:@"audio_url"];
        task.duration=[[taskInfo valueForKey:@"audio_time"] intValue];
        task.picUrl=[taskInfo valueForKey:@"pic_url"];
        task.isActive=[[taskInfo valueForKey:@"is_active"] intValue];
        task.update_time=[taskInfo valueForKey:@"update_time"];
        task.createTime=[taskInfo valueForKey:@"create_time"];
          task.tips = [taskInfo valueForKey:@"tips"];
        [taskArray addObject:task];
        task=NULL;
    }
    return taskArray;

}
//UI更改
-(Task *)getSysTask:(NSString *)id
{
    if([self checkValid]==NO)
        return NULL;
    if(id==NULL)
    {
        NSLog(@"%@,getSysTask,id is null.",GrowDataManager_TAG);
        return NULL;
    }
    NSString *nsql=[NSString stringWithFormat:@"select * from sys_task where id=%@;",id];
    NSArray *data=[self.theHelper searchData:[nsql UTF8String]];
    if(data==NULL||[data count]==0)
    {
        NSLog(@"%@,getSysTasks,data is null.",GrowDataManager_TAG);
        return NULL;
    }
    NSDictionary *taskInfo=[data objectAtIndex:0];
    if(taskInfo==NULL||[taskInfo count]==0)
        return NULL;
    Task *task=[[Task alloc] init];
    task.taskId=[taskInfo valueForKey:@"task_id"];
    task.type=[[taskInfo valueForKey:@"type"] intValue];
    task.expireTs=[[taskInfo valueForKey:@"expire"] intValue];
    task.title=[taskInfo valueForKey:@"title"];
    task.desc=[taskInfo valueForKey:@"description"];
    task.empirical=[[taskInfo valueForKey:@"empirical"] intValue];
    task.status=[[taskInfo valueForKey:@"status"] intValue];
    task.audioUrl=[taskInfo valueForKey:@"audio_url"];
    task.duration=[[taskInfo valueForKey:@"audio_time"] intValue];
    task.picUrl=[taskInfo valueForKey:@"pic_url"];
    task.isActive=[[taskInfo valueForKey:@"is_active"] intValue];
    task.update_time=[taskInfo valueForKey:@"update_time"];
    task.createTime=[taskInfo valueForKey:@"create_time"];
    task.tips = [taskInfo valueForKey:@"tips"];
    return task;
}
-(NSArray *)getTasks:(NSString *)ownerId targetUid:(NSString *)targetUid
{
    if([self checkValid]==NO)
        return NULL;
    if(ownerId==NULL||!([ownerId intValue]>0)||targetUid==NULL||!([targetUid intValue]>0))
    {
        NSLog(@"%@,getTasks,ownerId is null.",GrowDataManager_TAG);
        return NULL;
    }
    NSString *nsql=[NSString stringWithFormat:@"select * from task where owner_uid=%@ and target_uid=%@ order by create_time desc;",ownerId,targetUid];
    NSArray *data=[self.theHelper searchData:[nsql UTF8String]];
    if(data==NULL||[data count]==0)
    {
        NSLog(@"%@,getTasks,data is null.",GrowDataManager_TAG);
        return NULL;
    }
    NSMutableArray *taskArray=[[NSMutableArray alloc] init];
    for(int i=0;i<[data count];i++)
    {
        NSDictionary *taskInfo=[data objectAtIndex:i];
        if(taskInfo==NULL||[taskInfo count]==0)
            continue;
        Task *task=[[Task alloc] init];
        task.ownerUid=[taskInfo valueForKey:@"owner_uid"];
        task.targetUid=[taskInfo valueForKey:@"target_uid"];
        task.taskId=[taskInfo valueForKey:@"task_id"];
        task.type=[[taskInfo valueForKey:@"type"] intValue];
        task.expireTs=[[taskInfo valueForKey:@"expire_time"] intValue];
        task.title=[taskInfo valueForKey:@"title"];
        task.desc=[taskInfo valueForKey:@"description"];
        task.empirical=[[taskInfo valueForKey:@"exp"] intValue];
        task.status=[[taskInfo valueForKey:@"status"] intValue];
        task.audioUrl=[taskInfo valueForKey:@"audio_url"];
        task.duration=[[taskInfo valueForKey:@"audio_time"] intValue];
        task.picUrl=[taskInfo valueForKey:@"pic_url"];
        task.isActive=[[taskInfo valueForKey:@"is_active"] intValue];
        task.update_time=[taskInfo valueForKey:@"update_time"];
        task.createTime=[taskInfo valueForKey:@"create_time"];
        task.pushTime=[taskInfo valueForKey:@"push_time"];
        task.finishTime=[taskInfo valueForKey:@"finish_time"];
        task.randomCode=[taskInfo valueForKey:@"random_code"];
        task.action=[[taskInfo valueForKey:@"action"] intValue];
        [taskArray addObject:task];
        task=NULL;
    }
    return taskArray;

}
//UI更改
-(Growth *)getLatestGrowth:(NSString *)deviceUid
{
    if([self checkValid]==NO)
        return NULL;
    if(deviceUid==NULL||!([deviceUid intValue]>0))
    {
        NSLog(@"%@,getLatestGrowth,parameter is illegal.",GrowDataManager_TAG);
        return NULL;
    }
    NSString *nsql=[NSString stringWithFormat:@"select * from grow where deviceuid=%@ order by  create_time desc limit 1;",deviceUid];
    NSArray *data=[self.theHelper searchData:[nsql UTF8String]];
    if(data==NULL||[data count]==0)
        return NULL;
    NSDictionary *dataInfo=[data objectAtIndex:0];
    if(dataInfo==NULL||[dataInfo count]==0)
    {
        NSLog(@"%@,getLatestGrowth,dataInfo is null.",GrowDataManager_TAG);
        return NULL;
    }
    Growth *growth=[[Growth alloc] init];
    growth.semp=[[dataInfo valueForKey:@"semp"] intValue];
    growth.emp=[[dataInfo valueForKey:@"empirical"] intValue];
    growth.step=[[dataInfo valueForKey:@"step"] intValue];
    growth.blueNum=[[dataInfo valueForKey:@"blue_num"] intValue];
    growth.chatNum=[[dataInfo valueForKey:@"chat_num"] intValue];
    growth.callNum=[[dataInfo valueForKey:@"call_num"] intValue];
    growth.taskScore=[[dataInfo valueForKey:@"task_score"] intValue];
    growth.createTime=[dataInfo valueForKey:@"create_time"];
    growth.continousDays = [[dataInfo valueForKey:@"continousdays"] intValue];
    return growth;
}

//UI更改
-(Growth *)getTodayGrowth:(NSString *)deviceUid
{
    if([self checkValid]==NO)
        return NULL;
    if(deviceUid==NULL||!([deviceUid intValue]>0))
    {
        NSLog(@"%@,getTodayGrowth,parameter is illegal.",GrowDataManager_TAG);
        return NULL;
    }
    NSString *nowTs=[Tooler getTS];
    int year=[Tooler getYear:nowTs];
    int month=[Tooler getMonth:nowTs];
    int day=[Tooler getDay:nowTs];
    NSString *todayStr=[NSString stringWithFormat:@"%d-%d-%d 00:00:00",year,month,day];
    NSString *todayTs=[Tooler strToLocalTime:todayStr];
    NSString *nsql=[NSString stringWithFormat:@"select * from grow where deviceuid=%@ and  create_time>=%@ order by  create_time desc limit 1;",deviceUid,todayTs];
    NSArray *data=[self.theHelper searchData:[nsql UTF8String]];
    if(data==NULL||[data count]==0)
        return NULL;
    NSDictionary *dataInfo=[data objectAtIndex:0];
    if(dataInfo==NULL||[dataInfo count]==0)
    {
        NSLog(@"%@,getTodayGrowth,dataInfo is null.",GrowDataManager_TAG);
        return NULL;
    }
    Growth *growth=[[Growth alloc] init];
    growth.semp=[[dataInfo valueForKey:@"semp"] intValue];
    growth.emp=[[dataInfo valueForKey:@"empirical"] intValue];
    growth.step=[[dataInfo valueForKey:@"step"] intValue];
    growth.blueNum=[[dataInfo valueForKey:@"blue_num"] intValue];
    growth.chatNum=[[dataInfo valueForKey:@"chat_num"] intValue];
    growth.callNum=[[dataInfo valueForKey:@"call_num"] intValue];
    growth.taskScore=[[dataInfo valueForKey:@"task_score"] intValue];
    growth.createTime=[dataInfo valueForKey:@"create_time"];
    growth.continousDays = [[dataInfo valueForKey:@"continousDays"] intValue];
    return growth;
}
-(BOOL)addsysTasks:(NSArray *)tasks
{
    if([self checkValid]==NO)
        return NO;
    if(tasks==NULL||[tasks count]==0)
    {
        NSLog(@"%@,addsysTasks,tasks is null.",GrowDataManager_TAG);
        return NO;
    }
    NSMutableArray *sqlArray=[[NSMutableArray alloc] init];
    if(sqlArray==NULL)
    {
        NSLog(@"%@,addsysTasks,sqlArray is null.",GrowDataManager_TAG);
        return NO;
    }
    for(int i=0;i<[tasks count];i++)
    {
        Task *task=[tasks objectAtIndex:i];
        if(task==NULL)
        {
            NSLog(@"%@,addsysTasks,task is null.",GrowDataManager_TAG);
            continue;
        }
        //UI更改
        NSString *nsql=[NSString stringWithFormat:@"insert into sys_task(task_id,type,expire,title,description,empirical,status,audio_url,audio_time,pic_url,update_time,create_time,is_active,tips) values (%@,%d,%ld,\"%@\",\"%@\",%d,%d,\"%@\",%d,\"%@\",\"%@\",\"%@\",%d,\"%@\");",task.taskId,task.type,task.expireTs,task.title,task.desc,task.empirical,task.status,task.audioUrl,task.duration,task.picUrl,task.update_time,task.createTime,task.isActive, task.tips];
        [sqlArray addObject:nsql];
        nsql=NULL;
    }
    if([sqlArray count]==0)
    {
        NSLog(@"%@,addsysTasks,there is no sql to exec.",GrowDataManager_TAG);
        return NO;
    }
    BOOL result=[self.theHelper execSql:sqlArray];
    if(result)
    {
        NSLog(@"%@,addsysTasks success.",GrowDataManager_TAG);
    }
    else
    {
        NSLog(@"%@,addsysTasks fail.",GrowDataManager_TAG);
    }
    return result;

}
-(BOOL)updateSysTaskPic:(NSString *)pic_url taskId:(NSString *)taskId
{
    if([self checkValid]==NO)
        return NO;
    if(pic_url==NULL||taskId==NULL)
    {
        NSLog(@"%@,updateSysTaskPic,parameter is illegal.",GrowDataManager_TAG);
        return NO;
    }
    NSString *nsql=[NSString stringWithFormat:@"update sys_task set pic_url=\"%@\" where task_id=%@;",pic_url,taskId];
    BOOL result=[self.theHelper updateData:[nsql UTF8String] tableName:TABLE_SYSTASK];
    if(result)
    {
        NSLog(@"%@,updateSysTaskPic success.",GrowDataManager_TAG);
    }
    else
    {
        NSLog(@"%@,updateSysTaskPic fail.",GrowDataManager_TAG);
    }
    return result;
}
-(BOOL)updateSysTaskAudio:(NSString *)audio_url taskId:(NSString *)taskId
{
    if([self checkValid]==NO)
        return NO;
    if(audio_url==NULL||taskId==NULL)
    {
        NSLog(@"%@,updateSysTaskAudio,parameter is illegal.",GrowDataManager_TAG);
        return NO;
    }
    NSString *nsql=[NSString stringWithFormat:@"update sys_task set audio_url=\"%@\" where task_id=%@;",audio_url,taskId];
    BOOL result=[self.theHelper updateData:[nsql UTF8String] tableName:TABLE_SYSTASK];
    if(result)
    {
        NSLog(@"%@,updateSysTaskAudio success.",GrowDataManager_TAG);
    }
    else
    {
        NSLog(@"%@,updateSysTaskAudio fail.",GrowDataManager_TAG);
    }
    return result;
}
-(BOOL)updateHistoryTaskPic:(NSString *)pic_url taskId:(NSString *)taskId
{
    if([self checkValid]==NO)
        return NO;
    if(pic_url==NULL||taskId==NULL)
    {
        NSLog(@"%@,updateHistoryTaskPic,parameter is illegal.",GrowDataManager_TAG);
        return NO;
    }
    NSString *nsql=[NSString stringWithFormat:@"update task set pic_url=\"%@\" where task_id=%@;",pic_url,taskId];
    BOOL result=[self.theHelper updateData:[nsql UTF8String] tableName:TABLE_SYSTASK];
    if(result)
    {
        NSLog(@"%@,updateHistoryTaskPic success.",GrowDataManager_TAG);
    }
    else
    {
        NSLog(@"%@,updateHistoryTaskPic fail.",GrowDataManager_TAG);
    }
    return result;
}


-(BOOL)updateHistoryTaskAudio:(NSString *)audio_url randomCode:(NSString *)randomCode targetUid:(NSString *)targetUid audioHash:(NSString *)audioHash
{
    if([self checkValid]==NO)
        return NO;
    if(audio_url==NULL||randomCode==NULL||targetUid==NULL)
    {
        NSLog(@"%@,updateHistoryTaskAudio,parameter is illegal.",GrowDataManager_TAG);
        return NO;
    }
    User *user=[User sharedSingleton];
    if(user==NULL||user.uid==NULL)
        return NO;
    NSString *nsql=[NSString stringWithFormat:@"update task set audio_url=\"%@\",audio_hash=\"%@\" where random_code=\"%@\" and target_uid=%@ and owner_uid=%@;",audio_url,audioHash,randomCode,targetUid,user.uid];
    BOOL result=[self.theHelper updateData:[nsql UTF8String] tableName:TABLE_SYSTASK];
    if(result)
    {
        NSLog(@"%@,updateHistoryTaskAudio success.",GrowDataManager_TAG);
    }
    else
    {
        NSLog(@"%@,updateHistoryTaskAudio fail.",GrowDataManager_TAG);
    }
    return result;
}
-(BOOL)updatePushTaskPic:(NSString *)pic_url randomCode:(NSString *)randomCode
{
    if([self checkValid]==NO)
        return NO;
    if(pic_url==NULL||randomCode==NULL)
    {
        NSLog(@"%@,updatePushTaskPic,parameter is illegal.",GrowDataManager_TAG);
        return NO;
    }
    NSString *nsql=[NSString stringWithFormat:@"update task set pic_url=\"%@\" where random_code=\"%@\";",pic_url,randomCode];
    BOOL result=[self.theHelper updateData:[nsql UTF8String] tableName:TABLE_SYSTASK];
    if(result)
    {
        NSLog(@"%@,updatePushTaskPic success.",GrowDataManager_TAG);
    }
    else
    {
        NSLog(@"%@,updatePushTaskPic fail.",GrowDataManager_TAG);
    }
    return result;
}
-(BOOL)updatePushTaskAudio:(NSString *)audio_url randomCode:(NSString *)randomCode targetUid:(NSString *)targetUid audioHash:(NSString *)audioHash
{
    if([self checkValid]==NO)
        return NO;
    if(audio_url==NULL||randomCode==NULL||targetUid==NULL)
    {
        NSLog(@"%@,updatePushTaskAudio,parameter is illegal.",GrowDataManager_TAG);
        return NO;
    }
    User *user=[User sharedSingleton];
    if(user==NULL||user.uid==NULL)
        return NO;
    NSString *nsql=[NSString stringWithFormat:@"update task set audio_url=\"%@\",audio_hash=\"%@\" where random_code=\"%@\" and owner_uid=%@ and target_uid=%@;",audio_url,audioHash,randomCode,user.uid,targetUid];
    BOOL result=[self.theHelper updateData:[nsql UTF8String] tableName:TABLE_SYSTASK];
    if(result)
    {
        NSLog(@"%@,updatePushTaskAudio success.",GrowDataManager_TAG);
    }
    else
    {
        NSLog(@"%@,updatePushTaskAudio fail.",GrowDataManager_TAG);
    }
    return result;
}

-(BOOL)setTask:(Task *)task
{
    if([self checkValid]==NO)
        return NO;
    if(task==NULL||task.taskId==NULL)
    {
        NSLog(@"%@,setTask,parameter is illegal.",GrowDataManager_TAG);
        return NO;
    }
    NSString *nsql=[NSString stringWithFormat:@"insert into task(owner_uid,target_uid,task_id,type,status,title,description,exp,audio_url,audio_time,pic_url,random_code,expire_time,create_time,push_time,finish_time,update_time,action) values (%@,%@,%@,%d,%d,\"%@\",\"%@\",%d,\"%@\",%d,\"%@\",\"%@\",%ld,\"%@\",\"%@\",\"%@\",\"%@\",%d);",task.ownerUid,task.targetUid,task.taskId,task.type,task.status,task.title,task.desc,task.empirical,task.audioUrl,task.duration,task.picUrl,task.randomCode,task.expireTs,task.createTime,task.pushTime,task.finishTime,task.update_time,task.action];
    BOOL result=[self.theHelper insertData:[nsql UTF8String] tableName:TABLE_TASK];
    if(result)
    {
        NSLog(@"%@,setTask success.",GrowDataManager_TAG);
    }
    else
    {
        NSLog(@"%@,setTask fail.",GrowDataManager_TAG);
    }
    return result;

}
-(BOOL)finishTask:(Task *)task
{
    if([self checkValid]==NO)
        return NO;
    if(task==NULL||task.randomCode==NULL)
    {
        NSLog(@"%@,finishTask,task is null.",GrowDataManager_TAG);
        return NO;
    }
    NSString *nsql=[NSString stringWithFormat:@"update task set status=%d,audio_url=\"%@\",audio_time=%d,pic_url=\"%@\" where random_code=\"%@\" and owner_uid=%@;",task.status,task.audioUrl,task.duration,task.picUrl,task.randomCode,task.ownerUid];
    BOOL result=[self.theHelper updateData:[nsql UTF8String] tableName:TABLE_TASK];
    if(result)
    {
        NSLog(@"%@,finishTask success.",GrowDataManager_TAG);
    }
    else
    {
        NSLog(@"%@,finishTask fail.",GrowDataManager_TAG);
    }
    return result;
}
-(BOOL)confirmTask:(Task *)task
{
    if([self checkValid]==NO)
        return NO;
    if(task==NULL||task.randomCode==NULL)
    {
        NSLog(@"%@,confirmTask,task is null.",GrowDataManager_TAG);
        return NO;
    }
    NSString *nsql=[NSString stringWithFormat:@"update task set status=%d,audio_time=%d  where random_code=\"%@\" and owner_uid=%@;",task.status,task.duration,task.randomCode,task.ownerUid];
    BOOL result=[self.theHelper updateData:[nsql UTF8String] tableName:TABLE_TASK];
    if(result)
    {
        NSLog(@"%@,confirmTask success.",GrowDataManager_TAG);
    }
    else
    {
        NSLog(@"%@,confirmTask fail.",GrowDataManager_TAG);
    }
    return result;
}
-(NSString *)getMaxTaskTime:(NSString *)ownerId deviceUid:(NSString *)deviceUid
{
    if([self checkValid]==NO)
        return @"0";
    if(ownerId==NULL||!([ownerId intValue]>0)||deviceUid==NULL)
    {
        NSLog(@"%@,getMaxTaskTime,parameter is illegal.",GrowDataManager_TAG);
        return @"0";
    }
    NSString *nsql=[NSString stringWithFormat:@"select update_time from task where owner_uid=%@ and target_uid=%@ order by update_time desc limit 1;",ownerId,deviceUid];
    NSArray *data=[self.theHelper searchData:[nsql UTF8String]];
    if(data==NULL||[data count]==0)
    {
        NSLog(@"%@,getMaxTaskTime,data is null.",GrowDataManager_TAG);
        return @"0";
    }
    NSDictionary *dataInfo=[data objectAtIndex:0];
    if(dataInfo==NULL||[dataInfo count]==0)
    {
        NSLog(@"%@,getMaxTaskTime,dataInfo is null.",GrowDataManager_TAG);
        return @"0";
    }
    NSString *updateTime=[dataInfo valueForKey:@"update_time"];
    if(updateTime==NULL||[updateTime isEqualToString:@"(null)"]||[updateTime isEqualToString:@""])
        return @"0";
    return updateTime;
}
-(BOOL)addHistoryTasks:(NSArray *)taskArray
{
    if([self checkValid]==NO)
        return NO;
    if(taskArray==NULL||[taskArray count]==0)
    {
        NSLog(@"%@,addHistoryTasks,taskArray is null.",GrowDataManager_TAG);
        return NO;
    }
    NSMutableArray *sqlArray=[[NSMutableArray alloc] init];
    if(sqlArray==NULL)
        return NO;
    for(int i=0;i<[taskArray count];i++)
    {
        Task *task=[taskArray objectAtIndex:i];
        if(task==NULL)
            continue;
        NSString *nsql=[NSString stringWithFormat:@"select * from task where target_uid=%@ and  random_code=\"%@\";",task.targetUid,task.randomCode];
        NSArray *data=[self.theHelper searchData:[nsql UTF8String]];
        NSString *nsql1=NULL;
        if(data==NULL||[data count]==0)
        {
            nsql1=[NSString stringWithFormat:@"insert into task(owner_uid,target_uid,task_id,type,status,title,description,exp,audio_url,audio_time,pic_url,random_code,expire_time,create_time,push_time,finish_time,update_time,action) values (%@,%@,%@,%d,%d,\"%@\",\"%@\",%d,\"%@\",%d,\"%@\",\"%@\",%ld,\"%@\",\"%@\",\"%@\",\"%@\",%d);",task.ownerUid,task.targetUid,task.taskId,task.type,task.status,task.title,task.desc,task.empirical,task.audioUrl,task.duration,task.picUrl,task.randomCode,task.expireTs,task.createTime,task.pushTime,task.finishTime,task.update_time,task.action];
        }
        else
        {
            nsql1=[NSString stringWithFormat:@"update task set owner_uid=%@,task_id=%@,type=%d,status=%d,title=\"%@\",description=\"%@\",exp=%d,audio_url=\"%@\",audio_time=%d,pic_url=\"%@\",expire_time=%ld,create_time=\"%@\",push_time=\"%@\",finish_time=\"%@\",update_time=\"%@\",action=%d where target_uid=%@ and random_code=\"%@\";",task.ownerUid,task.taskId,task.type,task.status,task.title,task.desc,task.empirical,task.audioUrl,task.duration,task.picUrl,task.expireTs,task.createTime,task.pushTime,task.finishTime,task.update_time,task.action,task.targetUid,task.randomCode];
        }
        
        [sqlArray addObject:nsql1];
        nsql1=NULL;
    }
    if([sqlArray count]==0)
    {
        NSLog(@"%@,addHistoryTasks,there is no sql to exec.",GrowDataManager_TAG);
        return NO;
    }
    BOOL result=[self.theHelper execSql:sqlArray];
    if(result)
    {
        NSLog(@"%@,addHistoryTasks success.",GrowDataManager_TAG);
    }
    else
    {
        NSLog(@"%@,addHistoryTasks fail.",GrowDataManager_TAG);
    }
    return result;
}
-(BOOL)addPolicies:(NSArray *)policyArray
{
    if([self checkValid]==NO)
        return NO;
    if(policyArray==NULL||[policyArray count]==0)
    {
        NSLog(@"%@,addPolicies,policyArray is null.",GrowDataManager_TAG);
        return NO;
    }
    NSMutableArray *sqlArray=[[NSMutableArray alloc] init];
    for(int i=0;i<[policyArray count];i++)
    {
        ExpPolicy *policy=[policyArray objectAtIndex:i];
        if(policy==NULL)
            continue;
        NSString *nsql=[NSString stringWithFormat:@"insert into upgrade(level,exp) values (%d,%d);",policy.level,policy.exp];
        [sqlArray addObject:nsql];
        nsql=NULL;
    }
    BOOL result=[self.theHelper execSql:sqlArray];
    if(result)
    {
        NSLog(@"%@,addPolicies success.",GrowDataManager_TAG);
    }
    else
    {
        NSLog(@"%@,addPolicies fail.",GrowDataManager_TAG);
    }
    return result;

}

//UI更改
-(BOOL)addGrowth:(Growth *)growth deviceUid:(NSString *)deviceUid
{
    if([self checkValid]==NO)
        return NO;
    if(growth==NULL||deviceUid==NULL)
    {
        NSLog(@"%@,addGrowth,parameter is null.",GrowDataManager_TAG);
        return NO;
    }
    NSString *nsql=[NSString stringWithFormat:@"insert into grow(deviceuid,semp,empirical,step,blue_num,chat_num,call_num,task_score,create_time,continousDays) values (%@,%d,%d,%d,%d,%d,%d,%d,\"%@\",%d);",deviceUid,growth.semp,growth.emp,growth.step,growth.blueNum,growth.chatNum,growth.callNum,growth.taskScore,growth.createTime,growth.continousDays];
    BOOL result=[self.theHelper insertData:[nsql UTF8String] tableName:TABLE_GROW];
    if(result)
    {
        NSLog(@"%@,addGrowth success.",GrowDataManager_TAG);
    }
    else
    {
        NSLog(@"%@,addGrowth fail.",GrowDataManager_TAG);
    }
    return result;

}


-(NSArray *)getThemes
{
    if([self checkValid]==NO)
        return NULL;
    NSString *nsql=@"select * from theme";
    NSArray *data=[self.theHelper searchData:[nsql UTF8String]];
    if(data==NULL||[data count]==0)
        return NULL;
    NSMutableArray *themeArray=[[NSMutableArray alloc] init];
    if(themeArray==NULL)
        return NULL;
    for(int i=0;i<[data count];i++)
    {
        NSDictionary *themeInfo=[data objectAtIndex:i];
        if(themeInfo==NULL||[themeInfo count]==0)
            continue;
        Theme *theme=[[Theme alloc] init];
        theme.themeId=[themeInfo valueForKey:@"theme_id"];
        theme.name=[themeInfo valueForKey:@"name"];
        theme.desc=[themeInfo valueForKey:@"description"];
        theme.themePicUrl=[themeInfo valueForKey:@"theme_url"];
        theme.unablePicUrl=[themeInfo valueForKey:@"unable_url"];
        theme.labPicUrl=[themeInfo valueForKey:@"lab_url"];
        [themeArray addObject:theme];
        theme=NULL;
    }
    return themeArray;
}
-(BOOL)setTheme:(NSString *)deviceUid themeId:(NSString *)themeId
{
    if([self checkValid]==NO)
        return NO;
    if(deviceUid==NULL||themeId==NULL)
    {
        NSLog(@"%@,setTheme,parameter is illegal.",GrowDataManager_TAG);
        return NO;
    }
    NSString *nsql=[NSString stringWithFormat:@"update watch set theme_id=%@ where deviceuid=%@;",themeId,deviceUid];
    BOOL result=[self.theHelper updateData:[nsql UTF8String] tableName:TABLE_THEME];
    if(result)
    {
        NSLog(@"%@,setTheme success.",GrowDataManager_TAG);
    }
    else
    {
        NSLog(@"%@,setTheme fail.",GrowDataManager_TAG);
    }
    return result;
}
-(NSArray *)getThemeLocks:(NSString *)themeId
{
    if([self checkValid]==NO)
        return NULL;
    if(themeId==NULL)
        return NULL;
    NSString *nsql=[NSString stringWithFormat:@"select * from theme_lock where theme_id=%@;",themeId];
    NSArray *data=[self.theHelper searchData:[nsql UTF8String]];
    if(data==NULL||[data count]==0)
        return NULL;
    NSMutableArray *lockArray=[[NSMutableArray alloc] init];
    if(lockArray==NULL)
        return NULL;
    for(int i=0;i<[data count];i++)
    {
        NSDictionary *lockInfo=[data objectAtIndex:i];
        if(lockInfo==NULL||[lockInfo count]==0)
            continue;
        ThemeLock *lock=[[ThemeLock alloc] init];
        lock.level=[[lockInfo valueForKey:@"level"] intValue];
        lock.unlocPicUrl=[lockInfo valueForKey:@"unlock_url"];
        lock.lockPicUrl=[lockInfo valueForKey:@"lock_url"];
        [lockArray addObject:lock];
        lock=NULL;
    }
    return lockArray;
}
-(Theme *)getThemeInfo:(NSString *)themeId
{
    if([self checkValid]==NO)
        return NULL;
    if(themeId==NULL)
    {
        NSLog(@"%@,getThemeInfo,themeId is null.",GrowDataManager_TAG);
        return NULL;
    }
    NSString *nsql=[NSString stringWithFormat:@"select * from theme where theme_id=%@;",themeId];
    NSArray *data=[self.theHelper searchData:[nsql UTF8String]];
    if(data==NULL||[data count]==0)
        return NULL;
    NSDictionary *themeInfo=[data objectAtIndex:0];
    if(themeInfo==NULL||[themeInfo count]==0)
        return NULL;
    Theme *theme=[[Theme alloc] init];
    if(theme==NULL)
        return theme;
    theme.themeId=[themeInfo valueForKey:@"theme_id"];
    theme.name=[themeInfo valueForKey:@"name"];
    theme.desc=[themeInfo valueForKey:@"description"];
    theme.themePicUrl=[themeInfo valueForKey:@"theme_url"];
    theme.unablePicUrl=[themeInfo valueForKey:@"unable_url"];
    theme.labPicUrl=[themeInfo valueForKey:@"lab_url"];
    return theme;
    
}
-(ThemeLevel *)getLevelInfo:(NSString *)themeId level:(int)level
{
    if([self checkValid]==NO)
        return NULL;
    if(themeId==NULL||level<0)
    {
        NSLog(@"%@,getLevelInfo,themeId is null.",GrowDataManager_TAG);
        return NULL;
    }
    NSString *nsql=[NSString stringWithFormat:@"select * from theme_level where theme_id=%@ and level=%d;",themeId,level];
    NSArray *data=[self.theHelper searchData:[nsql UTF8String]];
    if(data==NULL||[data count]==0)
        return NULL;
    NSDictionary *levelInfo=[data objectAtIndex:0];
    if(levelInfo==NULL||[levelInfo count]==0)
        return NULL;
    ThemeLevel *themeTevel=[[ThemeLevel alloc] init];
    themeTevel.themeId=[levelInfo valueForKey:@"theme_id"];
    themeTevel.level=[[levelInfo valueForKey:@"level"] intValue];
    themeTevel.levelUrl=[levelInfo valueForKey:@"theme_url"];
    levelInfo=NULL;
    return themeTevel;
}
-(NSString *)getTaskAudioHash:(NSString *)randomCode targetUid:(NSString *)targetUid
{
    if([self checkValid]==NO)
        return NULL;
    if(randomCode==NULL||targetUid==NULL)
    {
        NSLog(@"%@,getTaskAudioHash,randomCode is null.",GrowDataManager_TAG);
        return NULL;
    }
    User *user=[User sharedSingleton];
    if(user==NULL||user.uid==NULL)
        return NULL;
    NSString *nsql=[NSString stringWithFormat:@"select audio_hash from task where random_code=\"%@\" and owner_uid=\"%@\" and target_uid=\"%@\";",randomCode,user.uid,targetUid];
    NSArray *data=[self.theHelper searchData:[nsql UTF8String]];
    if(data==NULL||[data count]==0)
        return NULL;
    NSDictionary *dataInfo=[data objectAtIndex:0];
    if(dataInfo==NULL||[dataInfo count]==0)
        return NULL;
    NSString *audioHash=[dataInfo valueForKey:@"audio_hash"];
    return audioHash;
}
-(BOOL)setWatchTheme:(NSString *)deviceUid themeId:(int)themeId themeLevel:(int)themeLevel
{
    if([self checkValid]==NO)
        return NO;
    if(deviceUid==NULL||themeId<=0)
    {
        NSLog(@"%@,setWatchTheme,param is illegal.",GrowDataManager_TAG);
        return NO;
    }
    NSString *nsql=NULL;
    if(themeLevel>=0)
        nsql=[NSString stringWithFormat:@"update watch set theme_id=%d,watch_theme=%d where deviceuid=%@;",themeId,themeLevel,deviceUid];
    else
        nsql=[NSString stringWithFormat:@"update watch set theme_id=%d where deviceuid=%@;",themeId,deviceUid];
    BOOL result=[self.theHelper updateData:[nsql UTF8String] tableName:TABLE_WATCH];
    if(result)
    {
        NSLog(@"%@,setWatchTheme success.",GrowDataManager_TAG);
    }
    else
    {
        NSLog(@"%@,setWatchTheme fail.",GrowDataManager_TAG);

    }
    return result;
}
@end
